package com.gobang.service.impl;

import com.gobang.service.BoardManager;
import com.gobang.service.GameAlgorithm;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

/**
 * 棋盘管理器实现类
 * 提供棋盘状态维护和操作功能
 * 
 * @author System
 * @since 1.0.0
 */
@Service
@RequiredArgsConstructor
public class BoardManagerImpl implements BoardManager {

    private static final Logger log = LoggerFactory.getLogger(BoardManagerImpl.class);

    private final GameAlgorithm gameAlgorithm;
    private final ObjectMapper objectMapper;

    /**
     * 棋盘大小
     */
    private static final int BOARD_SIZE = 15;

    @Override
    public boolean placeStone(int[][] board, int row, int col, int color) {
        log.debug("尝试在位置 ({}, {}) 放置颜色 {} 的棋子", row, col, color);
        
        if (!gameAlgorithm.isValidMove(board, row, col)) {
            log.warn("落子位置不合法: ({}, {})", row, col);
            return false;
        }
        
        // 验证颜色值
        if (color != 1 && color != 2) {
            log.warn("无效的棋子颜色值: {}", color);
            return false;
        }
        
        // 放置棋子
        board[row][col] = color;
        log.info("成功在位置 ({}, {}) 放置了颜色 {} 的棋子", row, col, color);
        return true;
    }

    @Override
    public boolean validateMove(int[][] board, int row, int col, int color, int currentPlayerColor) {
        log.debug("验证落子合法性: 位置({}, {}), 棋子颜色: {}, 当前玩家颜色: {}", 
            row, col, color, currentPlayerColor);
        
        // 检查是否轮到当前玩家
        if (color != currentPlayerColor) {
            log.warn("不是当前玩家的回合，当前玩家颜色: {}, 尝试放置颜色: {}", currentPlayerColor, color);
            return false;
        }
        
        // 检查位置是否合法
        return gameAlgorithm.isValidMove(board, row, col);
    }

    @Override
    public String getBoardJson(int[][] board) {
        try {
            String json = objectMapper.writeValueAsString(board);
            log.debug("棋盘状态转换为JSON成功");
            return json;
        } catch (JsonProcessingException e) {
            log.error("棋盘状态转换为JSON失败", e);
            return null;
        }
    }

    @Override
    public int[][] parseBoardFromJson(String boardJson) {
        try {
            int[][] board = objectMapper.readValue(boardJson, int[][].class);
            log.debug("从JSON解析棋盘状态成功");
            return board;
        } catch (JsonProcessingException e) {
            log.error("从JSON解析棋盘状态失败: {}", boardJson, e);
            return null;
        }
    }

    @Override
    public void resetBoard(int[][] board) {
        log.debug("重置棋盘");
        for (int i = 0; i < BOARD_SIZE; i++) {
            for (int j = 0; j < BOARD_SIZE; j++) {
                board[i][j] = 0;
            }
        }
        log.info("棋盘重置完成");
    }

    @Override
    public int getStone(int[][] board, int row, int col) {
        // 检查坐标是否有效
        if (row < 0 || row >= BOARD_SIZE || col < 0 || col >= BOARD_SIZE) {
            log.warn("获取棋子时坐标越界: ({}, {})", row, col);
            return -1; // 返回-1表示无效坐标
        }
        
        return board[row][col];
    }

    @Override
    public boolean isBoardFull(int[][] board) {
        log.debug("检查棋盘是否已满");
        
        for (int i = 0; i < BOARD_SIZE; i++) {
            for (int j = 0; j < BOARD_SIZE; j++) {
                if (board[i][j] == 0) {
                    log.debug("棋盘未满，位置 ({}, {}) 为空", i, j);
                    return false;
                }
            }
        }
        
        log.info("棋盘已满");
        return true;
    }
} 